using System;
using System.Windows.Forms;

namespace WillStott.MxAppToolbox
{
	/// <summary>
	/// Summary description for ToolbarOps.
	/// </summary>
	public class ToolbarOps
	{
		/// <summary>
		/// Enable ToolStrip button according it's corresponding menu item state
		/// </summary>
		/// <param name="bar">ToolStrip containing the button</param>
        public static void UpdateButtonState(ToolStrip bar)
		{
            foreach (ToolStripItem item in bar.Items)
			{
                ToolStripButton button = item as ToolStripButton;
                if (button != null)
                {
                    ToolStripMenuItem menu = button.Tag as ToolStripMenuItem;
                    if (menu != null)
                    {
                        menu.Image = button.Image;
                        button.Enabled = menu.Enabled;
                    }
                }
			}
		}

        /// <summary>
        /// Set MenuItem images from corresponding ToolStrip buttons
        /// </summary>
        /// <param name="bar">ToolStrip containing the buttons</param>
        public static void SetMenuItemImages(ToolStrip bar)
        {
            foreach (ToolStripItem item in bar.Items)
            {
                ToolStripButton button = item as ToolStripButton;
                if (button != null)
                {
                    ToolStripMenuItem menu = button.Tag as ToolStripMenuItem;
                    if (menu != null)
                    {
                        menu.Image = button.Image;
                    }
                }
            }
        }
		/// <summary>
		/// Pass click on Toolbar button to corresponding menu item
		/// </summary>
		/// <param name="e"></param>
        public static void ButtonClick(ToolStripItemClickedEventArgs e)
		{

            ToolStripMenuItem menu = e.ClickedItem.Tag as ToolStripMenuItem;
			if (menu != null)
			{
				menu.PerformClick();
			}
		}

	}
}
